using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AssetFrame.Message;
using System.Reflection;
using SysFrame.BP;
using SysFrame.Message;
using System.Data;

namespace AssetFrame.BP
{
    /// <summary>
    /// 资产分类
    /// </summary>
    public class BLLasset_a2
    {
        public Iasset_a2 interface_asset_a2;

        public BLLasset_a2()
        {
            object obj = BPBase.GetAchieve("AssetFrame.BP", "asset_a2");
            interface_asset_a2 = (Iasset_a2)obj;
        }


        /// <summary>
        /// 得到asset_a2实体
        /// </summary>
        /// <param name="msq">条件</param>
        /// <returns></returns>
        public List<Msasset_a2> Getasset_a2MsByQuery(MsQuery msq)
        {
            return interface_asset_a2.Getasset_a2MsByQuery(msq);
        }

        /// <summary>
        /// 通过ASSET_A2_AUTOID得到实体
        /// </summary>
        /// <param name="ASSET_A2_AUTOID">ID</param>
        /// <returns>实体</returns>
        public Msasset_a2 Getasset_a2MsByKey(string ASSET_A2_AUTOID)
        {
            MsQuery msquery = new MsQuery();
            msquery.strquery += " and ASSET_A2_AUTOID='" + ASSET_A2_AUTOID + "' ";
            List<Msasset_a2> lstData = Getasset_a2MsByQuery(msquery);
            return lstData[0];
        }

        /// <summary>
        ///  保存asset_a2信息
        /// </summary>
        /// <param name="ms">实体信息</param>
        /// <returns>消息</returns>
        public MsReturned Saveasset_a2(Msasset_a2 ms)
        {
            return interface_asset_a2.Saveasset_a2(ms);
        }

        /// <summary>
        /// 得到数据类型
        /// </summary>
        /// <param name="msquery">条件</param>
        /// <returns>数据类型</returns>
        public MsReturned Getasset_a2ByQuery(MsQuery msquery)
        {
            MsReturned msr = interface_asset_a2.Getasset_a2ByQuery(msquery);
            //如果需要显示资产数量
            if (msquery.typecode1 == "nub" && msr.data != null)
            {
                DataTable dt = ((DataSet)msr.data).Tables[0];
                BLLasset_a1 blla1 = new BLLasset_a1();
                List<string> groupstr = new List<string>();
                groupstr.Add("ASSET_A1_60");
                //得到资产数量
                MsQuery msqgroup = new MsQuery(msquery.strselect);
                msqgroup.strselect = "sum(a1.ASSET_A1_10)";//默认count(a1.ASSET_A1_AUTOID)
                DataTable treedt = blla1.Getasset_a1GroupDataByQuery(msqgroup, groupstr);
                var dic = new Dictionary<string, int>();
                foreach (DataRow rowitem in dt.Rows)
                {
                    int nub = 0;
                    string asset60 = rowitem["ASSET_A2_AUTOID"].ToString();
                    DataRow[] dr = treedt.Select(" ASSET_A1_60='" + rowitem["ASSET_A2_AUTOID"] + "'");
                    if (dr != null && dr.Count() != 0)
                    {
                        nub = Convert.ToInt32(dr[0]["value"].ToString());
                    }
                    dic.Add(asset60, nub);
                }
                SetTreeNub(dic, dt.Rows.Cast<DataRow>().ToArray(), dt);
                foreach (DataRow rowitem in dt.Rows)
                {
                    string asset60 = rowitem["ASSET_A2_AUTOID"].ToString();
                    string stra210 = rowitem["ASSET_A2_10"].ToString();
                    rowitem["ASSET_A2_10"] = stra210 + "[" + dic[asset60] + "]";
                }
            }
            return msr;
        }

        private void SetTreeNub(Dictionary<string, int> dic, DataRow[] rows, DataTable dt)
        {
            foreach (DataRow rowitem in rows)
            {
                string asset60 = rowitem["ASSET_A2_AUTOID"].ToString();
                string pid = rowitem["ASSET_A2_PARENTID"].ToString();
                if (pid != "" && pid != "0")
                {
                    if (dic.ContainsKey(pid))
                    {
                        dic[pid] = dic[pid] + dic[asset60];
                    }
                    else
                    {
                        dic.Add(pid, 0);
                    }
                    SetTreeNub(dic, dt.Select(" ASSET_A2_AUTOID='" + pid + "'"), dt);
                }
            }
        }

        /// <summary>
        /// 删除asset_a2
        /// </summary>
        /// <param name="msq">条件</param>
        /// <returns></returns>
        public MsReturned Deleteasset_a2ByQuery(MsQuery msq)
        {
            return interface_asset_a2.Deleteasset_a2ByQuery(msq);
        }
        /// <summary>
        /// 删除asset_a2
        /// </summary>
        /// <param name="msq">条件</param>
        /// <returns></returns>
        public MsReturned Deleteasset_a2ByKey(string ASSET_A2_AUTOID)
        {
            MsQuery msquery = new MsQuery();

            if (ASSET_A2_AUTOID.IndexOf(",") > 0)
            {
                msquery.strquery += " and ASSET_A2_AUTOID in (" + ASSET_A2_AUTOID + ") ";
            }
            else
            {
                msquery.strquery += " and ASSET_A2_AUTOID='" + ASSET_A2_AUTOID + "' ";
            }
            return interface_asset_a2.Deleteasset_a2ByQuery(msquery);
        }
    }
}
